﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.member
{
    public partial class shop_join : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var userInfo = WebMasterCookie.GetCookie();
            if (userInfo == null)
            {
                Response.Redirect("/login.aspx");
            }
            AjaxPro.Utility.RegisterTypeForAjax(typeof(shop_join));
        }
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetNewsList(int pageIndex, int pageSize, string sorting)
        {
            var userInfo = WebMasterCookie.GetCookie();
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            if (userInfo != null)
            {
                try
                {
                    StringBuilder builder = new StringBuilder();
                    var dicWhere = new Dictionary<string, object>();
                    dicWhere.Add("NC_Code", "010");
                    dicWhere.Add("Client_Id", userInfo.Id);
                    var dataTable = NewsInfoBll.GetNewsInfoTable(pageIndex, pageSize, "*", sorting + " desc", dicWhere, CommonConst.DB_Name);
                    foreach (DataRow row in dataTable.Rows)
                    {
                        string newsId = EncryptHelper.EncryptString(row["News_Id"].ToString());
                        string title = row["News_Title"].ToString();
                        string cuttitle = CommonBll.CutString(title, 70, true);
                        builder.Append("<ul>");
                        builder.AppendFormat("<li class=\"title\"><a href=\"article_add/{0}.html\" title=\"{1}\">{2}</a></li>", newsId, title, cuttitle);
                        builder.AppendFormat("<li class=\"click\">{0}</li>", row["News_Times"]);
                        builder.AppendFormat("<li class=\"datetime\">{0}</li>", Convert.ToDateTime(row["News_Date"]).ToString("yyyy-MM-dd HH:mm"));
                        var audit = int.Parse(row["News_Status"].ToString()) == 0
                                        ? "<span style=\"color:red;\">未通过</span>"
                                        : "<span style=\"color:green;\">已通过</span>";
                        builder.AppendFormat("<li class=\"audit\">{0}</li>", audit);
                        builder.AppendFormat("<li class=\"operate\"><a href=\"article_add/{0}.html\">修改</a>&nbsp;<a href=\"javascript:void(0);\" onclick=\"onDelete('{0}')\">删除</a>", newsId);
                        builder.AppendFormat("&nbsp;<a target=\"_blank\" href=\"/html/preview/{0}/{1}.html\">预览</a>",
                                             row["NC_Code"], newsId);
                        builder.Append("</li>");
                        builder.Append("</ul>");
                    }
                    jsonResult.Add("isreload", true);
                    jsonResult.Add("msg", builder.ToString());
                }
                catch (Exception ex)
                {
                    jsonResult.Clear();
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", ex.Message);
                }

            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请退出重新登录！");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetNewsCount()
        {
            var userInfo = WebMasterCookie.GetCookie();
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            if (userInfo != null)
            {
                var count = NewsInfoBll.GetNewsInfoCount(" NC_Code='010' and Client_Id=" + userInfo.Id, CommonConst.DB_Name);
                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", count);
            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请退出重新登录！");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string Delete(string id)
        {
            var userInfo = WebMasterCookie.GetCookie();
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            if (userInfo != null)
            {
                try
                {
                    NewsInfoBll.DeleteNewsInfo(EncryptHelper.DecryptString(id), userInfo.Id,  CommonConst.DB_Name);
                    Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                    dicHandleLog.Add("user_id", userInfo.Id);
                    dicHandleLog.Add("computername", userInfo.Computername);
                    dicHandleLog.Add("computerip", userInfo.Computerip);
                    dicHandleLog.Add("handle_note", "删除招商加盟");
                    dicHandleLog.Add("obj_guid", "News_Id=" + id);
                    LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);

                    jsonResult.Add("isreload", true);
                    jsonResult.Add("msg", "删除成功！");
                }
                catch (Exception ex)
                {
                    jsonResult.Clear();
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", ex.Message);
                }

            }
            else
            {
                jsonResult.Clear();
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请退出重新登录！");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
